package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TurnoverRate;
import com.ruoyi.system.service.ITurnoverRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 成交率Controller
 *
 * @author ruoyi
 * @date 2022-04-13
 */
@RestController
@RequestMapping("/system/rate")
public class TurnoverRateController extends BaseController {
    @Autowired
    private ITurnoverRateService turnoverRateService;

    /**
     * 查询成交率列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TurnoverRate turnoverRate) {
        startPage();
        List<TurnoverRate> list = turnoverRateService.selectTurnoverRateList(turnoverRate);
        return getDataTable(list);
    }

    @GetMapping("/info")
    public AjaxResult info(){
        List<Integer> num = new ArrayList<>();
        TurnoverRate turnoverRate = new TurnoverRate();
        turnoverRate.setName("今日成交率");
        List<TurnoverRate> list = turnoverRateService.selectTurnoverRateList(turnoverRate);
        num.add(list.get(0).getValue());
        turnoverRate.setName("本月成交率");
        List<TurnoverRate> list1 = turnoverRateService.selectTurnoverRateList(turnoverRate);
        num.add(list1.get(0).getValue());
        turnoverRate.setName("今年成交率");
        List<TurnoverRate> list2 = turnoverRateService.selectTurnoverRateList(turnoverRate);
        num.add(list2.get(0).getValue());
        return AjaxResult.success(num);
    }

    /**
     * 获取成交率详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(turnoverRateService.selectTurnoverRateById(id));
    }

    /**
     * 新增成交率
     */
    @PostMapping
    public AjaxResult add(@RequestBody TurnoverRate turnoverRate) {
        TurnoverRate res = new TurnoverRate();
        res.setName(turnoverRate.getName());
        List<TurnoverRate> list = turnoverRateService.selectTurnoverRateList(res);
        Assert.isTrue(list.size()==0,"该类型已有数据");
        turnoverRate.setCreateUser(SecurityUtils.getUserId().intValue());
        return toAjax(turnoverRateService.insertTurnoverRate(turnoverRate));
    }

    /**
     * 修改成交率
     */
    @PutMapping
    public AjaxResult edit(@RequestBody TurnoverRate turnoverRate) {
        return toAjax(turnoverRateService.updateTurnoverRate(turnoverRate));
    }

    /**
     * 删除成交率
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(turnoverRateService.deleteTurnoverRateByIds(ids));
    }
}
